import Vue from 'vue'
import VueRouter from 'vue-router'
import LoginView from '../views/LoginView.vue'
import EmployeeListView from '../views/EmployeeListView.vue'
import OrderManagementView from '../views/OrderManagementView.vue'
import CustomerManagementView from '../views/CustomerManagementView.vue'
import ContractManagementView from '../views/ContractManagementView.vue'
import ContractAddView from '../views/ContractAddView.vue'
import FactoryManagementView from '@/views/FactoryManagementView.vue'
import DeviceManagementView from '@/views/DeviceManagementView.vue'
import { getFirstAccessibleMenu } from '@/utils/permission'
import NoPermissionView from '@/views/NoPermissionView.vue'
Vue.use(VueRouter)
// 解决编程式路由往同一地址跳转时会报错的情况
const originalPush = VueRouter.prototype.push;
const originalReplace = VueRouter.prototype.replace;

// push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalPush.call(this, location, onResolve, onReject);
  return originalPush.call(this, location).catch(err => err);
};

//replace
VueRouter.prototype.replace = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalReplace.call(this, location, onResolve, onReject);
  return originalReplace.call(this, location).catch(err => err);
};

const routes = [
  {
    path: '/',
    name: 'login',
    component: LoginView
  },
  {
    path: '/employee-list',
    name: 'employee-list',
    component: EmployeeListView
  },
  {
    path: '/order-management',
    name: 'order-management',
    component: OrderManagementView
  },
  {
    path: '/customer-management',
    name: 'customer-management',
    component: CustomerManagementView
  },
  {
    path: '/contract-management',
    name: 'contract-management',
    component: ContractManagementView
  },
  {
    path: '/contract-add',
    name: 'contract-add',
    component: ContractAddView
  },
  {
    path: '/factory-management',
    name: 'factory-management',
    component: FactoryManagementView
  },
  {
    path: '/device-management',
    name: 'device-management',
    component: DeviceManagementView
  },
  {
    path: '/no-permission',
    name: 'no-permission',
    component: NoPermissionView
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
  scrollBehavior(to, from, saveNotRequired) {
    //始终滚到顶部
    return { x: 0, y: 0 }
  }
})

// 全局前置守卫：检查登录状态
router.beforeEach((to, from, next) => {
  const publicPages = ['/']; // 不需要登录即可访问的页面路径
  const authRequired = !publicPages.includes(to.path);
  const token = localStorage.getItem('token'); // 从 localStorage 获取 token

  if (authRequired && !token) {
    // 如果需要授权但没有token，重定向到登录页
    next('/');
  } else if (to.path === '/' && token) {
    // 如果是登录页，且已经有token，根据用户权限跳转到第一个可访问的菜单
    const userRole = localStorage.getItem('role');
    const firstMenu = getFirstAccessibleMenu(userRole);
    console.log('用户角色:', userRole, '跳转到:', firstMenu);
    next(firstMenu);
  } else {
    // 其他情况正常跳转
    next();
  }
});

export default router
